package com.nmcc.app_adherence;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.UUID;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class HouseDetailsActivity extends Activity {
	
	private static String CSV_NAME = "GPSData.csv";
	private static String CSV_NAME_PERSONS = "Persons.csv";
	private static String hs_id;// = db.GetSetting(3);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_house_details);
		
		MySQLiteHelper db = new MySQLiteHelper(this);
		hs_id= db.GetSetting(3);
		
        Button b55 = (Button) findViewById(R.id.btnOdksurveyMonitoring);
        b55.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {
        	   //first take data to odk
        	   
        	   //ExportGPSDataToCSVTakeToODKDirectNewMethod(arg0,1);
        	   
        	   
        	   ExportGPSDataToCSVTakeToODKDirect(arg0);
        	   
        	   
        	   
        	   
        	   //launchOdkSurvey(arg0);
        	   //Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
        	   Toast.makeText(getApplicationContext(), "Reminder: This phone shall ONLY contain MONITORING forms in ODK Survey.", 5000).show();
        	   
        	   Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("org.opendatakit.survey.android");
        	   startActivity(LaunchIntent);
        	   
           }
        });
        
        Button bpers = (Button) findViewById(R.id.btnPersons);
        bpers.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {
        	   Go_To_PersonsList_Linker(arg0);
           }
        });
        
        //Button bfin = (Button) findViewById(R.id.btnFinalizeHouse);
       // bfin.setOnClickListener(new View.OnClickListener() {
         //  public void onClick(View arg0) {
        	   //finalisehouse(arg0);
          // }
        //});
        
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.house_details, menu);
		return true;
	}

	public void Go_To_PersonsList_Linker(View view)
    {
	   //'navigate to another screen'
	   final Context context = this;

       //give values
	   Intent intent = new Intent(context, PersonsListingActivity.class);
	   //intent.putExtra("cluster","22");
	    startActivity(intent);

    }
	
	 public void ExportGPSDataToCSVTakeToODKDirect(View view){
		   MySQLiteHelper db = new MySQLiteHelper(this);
		   //to avoid overwriting in odk survey just send to sd card folder
	      // add Books
		   
	      //db.DropAllTables();
		   DeleteODKHouseNewCSVFile();
		   //GPSGiveOdkAs1DontTakeAs2PersonsGiveOdkAs3DontTakeAs4Net5Mem6Mal7Hshnew8Adhere9NewHouse10AdheHsPhone11
	     //Toast.makeText(getApplicationContext(), "WARNING! This will overwrite any existing csv files in ODK. For testing purposes ONLY.", 5000).show();
		   //"GPSData-" + data('household_id') + ".csv"
		   //CSV_NAME="GPSData-" + hs_id + ".csv";
		   //CSV_NAME_PERSONS=lll;
		   
	    db.ExportGPSDataToCSVODKDirect(CSV_NAME,10,hs_id );//new house old
	    //db.ExportGPSDataToCSVODKDirect(CSV_NAME,11);//adherence phone

	   db.ExportPersonsToCSVDirectToODKTestingOnly(CSV_NAME_PERSONS,5,hs_id);//nets
	   db.ExportPersonsToCSVDirectToODKTestingOnly(CSV_NAME_PERSONS,6,hs_id);//member
	   db.ExportPersonsToCSVDirectToODKTestingOnly(CSV_NAME_PERSONS,7,hs_id);//malaria
	   db.ExportPersonsToCSVDirectToODKTestingOnly(CSV_NAME_PERSONS,12,hs_id);//travel
	   
	   //db.ExportPersonsToCSVDirectToODKTestingOnly(CSV_NAME_PERSONS,8);//household new
	   //db.ExportPersonsToCSVDirectToODKTestingOnly(CSV_NAME_PERSONS,9);//member adherence
	    
	    Toast.makeText(getApplicationContext(), "Files successfully exported to csv. Please check this data in the ODK Survey", 5000).show();

	}   
	
	public void DeleteODKHouseNewCSVFile(){
		 //folderInSDCardDestin = "/opendatakit/survey/tables/household/forms/household_new/";
		String StroutputFile1 =Environment.getExternalStorageDirectory() + "/opendatakit/survey/tables/household/forms/household_new/GPSData.csv";
		String StroutputFile2 =Environment.getExternalStorageDirectory() + "/opendatakit/survey/tables/household_member/forms/malaria/";
		String StroutputFile3 =Environment.getExternalStorageDirectory() + "/opendatakit/survey/tables/household_nets/forms/nets/Persons.csv";
		String StroutputFile4 =Environment.getExternalStorageDirectory() + "/opendatakit/survey/tables/household_member/forms/member/Persons.csv";
		String StroutputFile5 =Environment.getExternalStorageDirectory() + "/opendatakit/survey/tables/household_member_travel/forms/travel/Persons.csv";
			
		
		// before we open the file check to see if it already exists
			   File fileOutputm1 = new File(StroutputFile1);
	        if (fileOutputm1.exists()) {
	            fileOutputm1.delete();
	        }
     
	        File fileOutputm2 = new File(StroutputFile2);
	        if (fileOutputm2.exists()) {
	            fileOutputm2.delete();
	        }
	        File fileOutputm3 = new File(StroutputFile3);
	        if (fileOutputm3.exists()) {
	            fileOutputm3.delete();
	        }
	        File fileOutputm4 = new File(StroutputFile4);
	        if (fileOutputm4.exists()) {
	            fileOutputm4.delete();
	        }
	        
	        File fileOutputm5 = new File(StroutputFile5);
	        if (fileOutputm5.exists()) {
	            fileOutputm5.delete();
	        }
	}
	
	public void finalisehouse(View vew){
		
		Toast.makeText(getApplicationContext(), "Select the finalise household options.", 5000).show();
 	   
		
		final Context context1 = this;
		
		
        //give values
 	   Intent intent1 = new Intent(context1, HouseDetailsActivity.class);
        finish();
	}
	
	

	
}
